{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "from threading import Thread, Lock"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thread 0 started\n",
      "thread 1 started\n",
      "thread 2 started\n",
      "thread 3 started\n",
      "thread 4 started\n",
      "thread 5 started\n",
      "thread 6 started\n",
      "thread 7 started\n",
      "thread 8 started\n",
      "thread 9 started\n",
      "thread 9 finished\n",
      "thread 8 finished\n",
      "thread 7 finished\n",
      "thread 6 finished\n",
      "thread 5 finished\n",
      "thread 4 finished\n",
      "thread 2 finished\n",
      "thread 1 finished\n",
      "thread 0 finished\n",
      "thread 3 finished\n",
      "completed\n",
      "[9, 8, 7, 6, 5, 4, 2, 1, 0, 3]\n"
     ]
    }
   ],
   "source": [
    "test_arr = []\n",
    "lock = Lock()\n",
    "\n",
    "def test_func(i):\n",
    "    print(f\"thread {i} started\")\n",
    "    time.sleep(1)\n",
    "    lock.acquire()\n",
    "    test_arr.append(i)\n",
    "    lock.release()\n",
    "    print(f\"thread {i} finished\")\n",
    "\n",
    "\n",
    "threads = []\n",
    "\n",
    "for i in range(10):\n",
    "    t = Thread(target=test_func, args=(i, ))\n",
    "    t.start()\n",
    "    threads.append(t)\n",
    "\n",
    "for t in threads:\n",
    "    t.join()\n",
    "\n",
    "print(\"completed\")\n",
    "print(test_arr)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[40, 10, 0, 30, 20, 50, 70, 60, 80, 90, 100, 140, 130, 110, 120, 150, 180, 190, 170, 160]\n"
     ]
    }
   ],
   "source": [
    "from concurrent.futures import ThreadPoolExecutor, wait, as_completed\n",
    "import time\n",
    "\n",
    "def test(param):\n",
    "    time.sleep(1)\n",
    "    return param * 10\n",
    "\n",
    "pool = ThreadPoolExecutor(max_workers=5)\n",
    "tasks = [pool.submit(test, i) for i in range(20)]\n",
    "results = [x.result() for x in as_completed(tasks)]\n",
    "print(results)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}